package com.budwk.app.enterprise.services.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.base.utils.PoiTlUtil;
import com.budwk.app.base.utils.StrUtil;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.app.enterprise.models.BusinessInformation;
import com.budwk.app.enterprise.services.BusinessinformationService;
import com.budwk.starter.security.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.upload.TempFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@IocBean(args = {"refer:dao"})
public class BusinessinformationServiceImpl extends BaseServiceImpl<BusinessInformation> implements BusinessinformationService {
    public BusinessinformationServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public List<BusinessInformation> listCompanys() {
        return this.query();
    }

    @Override
    public void importCompanyData(TempFile file) {
        // 1 获取表格的单元格
        Sheet sheet = PoiTlUtil.getBookSheet(file);
        // 2 获取导入的数据
        List<BusinessInformation> addData = new ArrayList<>(); // 修改的数据
        List<BusinessInformation> updateData = new ArrayList<>(); // 新增的数据
        getBusinessInfoInportData(sheet, addData, updateData);
        if (addData.size() > 0) {
            this.fastInsert(addData);
        }
        if (updateData.size() > 0) {
            this.updateIgnoreNull(addData);
        }
    }

    private void getBusinessInfoInportData(Sheet sheet, List<BusinessInformation> addData, List<BusinessInformation> updateData) {
        // 获取所有导入的数据
        List<JSONObject> importContents = PoiTlUtil.getImportContent(sheet, 2);
        if (importContents.isEmpty()) {
            return;
        }
        Cnd cnd = Cnd.NEW();
        SqlExpressionGroup where = cnd.where();
        // 获取数据字典项
        for (JSONObject json : importContents) {
            // 处理空字符
            BusinessInformation data = JSONObject.toJavaObject(json, BusinessInformation.class);
            // 处理空字符
            StrUtil.handleStrTrim(data);
            String companyName = data.getCompanyName().trim();
            // 判断企业是否存在
            where.andEquals(BusinessInformation::getCompanyName, companyName).andEquals(BusinessInformation::getDelFlag, false);
            BusinessInformation information = this.fetch(cnd);
            data.setCompanyName(companyName);
            if (information != null) {
                data.setId(information.getId());
                data.setUpdatedBy(SecurityUtil.getUserId());
                data.setUpdatedAt(System.currentTimeMillis());
                updateData.add(data);
                continue;
            }
            data.setCreatedBy(SecurityUtil.getUserId());
            data.setCreatedAt(System.currentTimeMillis());
            data.setUpdatedBy(SecurityUtil.getUserId());
            data.setUpdatedAt(System.currentTimeMillis());
            // 解析特殊人员类型
            addData.add(data);
        }
    }
}
